package net.alteiar.algorithms;

import java.awt.Color;

import net.alteiar.images.BasicImage;
import net.alteiar.images.metadata.ImageInfo;

public class RGBExtractor {
	private static final int EXTRACT_RED = 0;
	private static final int EXTRACT_GREEN = 1;
	private static final int EXTRACT_BLUE = 2;

	public static ImageInfo extractRed(BasicImage im) {
		return extractColor(im, EXTRACT_RED);
	}

	public static ImageInfo extractGreen(BasicImage im) {
		return extractColor(im, EXTRACT_GREEN);
	}

	public static ImageInfo extractBlue(BasicImage im) {
		return extractColor(im, EXTRACT_BLUE);
	}

	private static ImageInfo extractColor(BasicImage im, int extract) {
		ImageInfo result = new ImageInfo(im, 1);

		for (int x = 0; x < result.getWidthSquare(); x++) {
			for (int y = 0; y < result.getHeightSquare(); y++) {
				Color current = new Color(im.getImage().getRGB(x, y));

				Color target = null;
				int color = 0;
				switch (extract) {
				case EXTRACT_RED:
					target = new Color(current.getRed(), 0, 0, 255);
					break;
				case EXTRACT_GREEN:
					color = current.getGreen();
					target = new Color(color, color, color, 255);
					break;
				case EXTRACT_BLUE:
					// System.out.println(current.getBlue());
					color = current.getBlue();
					// if (color < 127) {
					// color = 0;
					// } else {
					// color = 255;
					// }
					target = new Color(color, color, color, 255);
					break;
				}

				result.setValue(x, y, target.getRGB());
			}
		}

		return result;
	}
}
